<?php
/**
 *OPENSHOP 管理中心聚集页设定程序
 *@package     OpenShop
 *@version     $Id: editxmldb.php,v 1.0 2009/07/29 11:18:17 modified $
 *@author      杨敬君 <aqkt_436@hotmail.com>
 */
define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once("Cdycommand.php");
$lib=$_REQUEST['lib'];
$editsort=$_REQUEST['editsort'];
$dytype=$_REQUEST['type'];
$sort=$_REQUEST['sort'];
$dy_id=$_REQUEST['dy_id'];
$ve=$_REQUEST['ve'];
$nodeid=$_REQUEST['nodeid'];
if(is_numeric($editsort)==FALSE){
	pagealert("非法操作：输入参数必须为数字！");
}
if($editsort==$sort){
    pagealert("非法操作：输入的参数与原排序号相同！");
}
if(is_dyidOnly($dy_id)==0){
    pagealert("非法操作：数据未入录！");
}else{	
	if(is_xmlSortOlny()){
		pagealert("序号已存在！");	      
	}else{	
	if(is_SortOnly($lib,$editsort)==0){ 
	     $sql="update ".$os->table('dynamic')." set dy_sort='$editsort' where dy_Project='$lib' and dy_Type='$dytype' and dy_id='$dy_id'";
         $db->query($sql);      
         $doc = new DOMDocument();    
         $doc->load($config_file);
         $libs = $doc->getElementsByTagName("lib");
	     foreach( $libs as $lib_node){	
		      if($lib_node->getAttribute("id")==$lib){ 		  
			      $data = $lib_node->getElementsByTagName("data");
                  $data->item($nodeid)->setAttribute('Sort',$editsort);
	          }
	     }
	    $doc->save(ROOT_PATH . 'themes/' . $GLOBALS['_CFG']['template'].'/library/config.xml'); 
        pagealert("设置成功！");	  
	}else{
	    pagealert("序号已存在！");
    }	
	}
}
/**
 * 判断项目库是序号是否存在于数据库
 * @param   string      $lib 项目库名
 * @param   string      $sort 排序
 * @return  boolean
 */
function is_SortOnly($lib,$sort)
{
    $res = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('dynamic') .
                                  " WHERE dy_Project = '$lib' and dy_Sort='$sort'");
    return $res;

}
/**
 * XML配置文件是否存在排序
 * @return true/null
 */
function is_xmlSortOlny(){   
    global $config_file;
    global $lib;
    global $editsort;
    $Comparison=new DOMDocument();
    $Comparison->load($config_file);
    $libs = $Comparison->getElementsByTagName("lib");    
    foreach( $libs as $lib_node){
      if($lib==$lib_node->getAttribute("id")){
      	$slibs=$lib_node->getElementsByTagName("data");
      		foreach ($slibs as $slib_node =>$key){	
      			if($editsort==$key->getAttribute("Sort")){
      				$result=TRUE;
      			}
      		}
        } 
    }    
    return $result;
}
/*
 * 页面提示信息
 */
function Pagealert($meg){
	global $lib;
	$link[0]['text'] = '返回上一页';
    $link[0]['href'] = "createdynamic.php?id=$lib";
    sys_msg($meg, 0, $link);
}    
?>